package com.bw.zg3park2212.news

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bw.base.entity.NewsAddParam
import com.bw.base.entity.NewsEntity
import com.bw.base.entity.NewsEntityItem
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(val apiService: ApiService):BaseViewModel() {
    fun getnews():Flow<PagingData<NewsEntityItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            NewsSocuer(apiService)
        }).flow.cachedIn(viewModelScope)
    }
    val newsdel=MutableLiveData<Boolean>()
    fun newdel(id:Int){
        httpRequst(
            {apiService.getnewsdel(id)},
            newsdel::postValue)
    }
    var newssearchSuccse=MutableLiveData<NewsEntity>()
    fun newssearch(key:String){
        httpRequst(
            {apiService.getnewssearch(key)},
            newssearchSuccse::postValue)
    }
    var newsaddSuucse=MutableLiveData<NewsEntityItem>()
    fun newsadd(newsAddParam: NewsAddParam){
        httpRequst(
            {apiService.getnewsadd(newsAddParam)},
            newsaddSuucse::postValue)
    }
    var newsupSuccse=MutableLiveData<NewsEntityItem>()
    fun newsup(id:Int,body:NewsAddParam){
        httpRequst(
            {apiService.getnewsup(id,body)},
            newsupSuccse::postValue)

    }
}